<?php
/**
 * Created by PhpStorm.
 * User: Adminstratot
 * Date: 2018/11/1
 * Time: 17:10
 */
namespace Admin\Model;
use Think\Model;

class CateModel extends Model{
    protected $connection = 'DB_DATA';//连接默认数据库时不写即可

    protected $trueTableName = 'cate';
    protected $insertFields = 'name,intro,pid';

    protected $_auto = array(
        //array(完成字段1,完成规则,[完成条件,附加规则]),
        array('time','time',3,'function'),
    );

    protected $_validate = array(
        //array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间])
        array('name','1,50','栏目名称不规范!',1,'length',3),
        array('name','','该栏目已经存在了',1,'unique',3),
        array('intro','1,50','栏目介绍长度不符合要求!',1,'length',3),
    );

    public function gettree($id=0,$lev=0){
        $tree=array();
        foreach($this->select() as $v){
            if($v['pid']==$id){
                $v['lev']=$lev;
                $tree[]=$v;
                $tree=array_merge($tree,$this->gettree($v['cid'],$lev+1));	//array_merge拼接数组
            }
        }
        return $tree;
    }
}
?>